import pytest
import requests

from data9_24.src.Api_util.conftest import api_base

class TestApiHandler01:
    sessionId = None
    userId = None
    id = None
    commodityId = None
    price = None


    @pytest.fixture(autouse=True)
    def setup(self):
        self.tools = api_base()
    def test_api_login_001(self):
        post_resp = self.tools.post(
            path = 'small/user/v1/login',
            json = {"phone": "17518836160", "pwd": "123456Aa"}
        )
        print("维度电商登录响应状态码:", post_resp.status_code)
        print("维度电商登录响应数据:", post_resp.text)
        # 断言状态码为200
        assert post_resp.status_code == 200
        # 验证响应结构
        # 断言响应中缺少result字段
        assert 'result' in post_resp.json()
        # 断言响应中有没有sessionId和userId字段"
        assert 'sessionId' in post_resp.json()['result']
        assert 'userId' in post_resp.json()['result']
        print("sessionId值:", post_resp.json()['result']['sessionId'])
        print("sessionId值:", post_resp.json()['result']['userId'])
        TestApiHandler01.sessionId = post_resp.json()['result']['sessionId']
        TestApiHandler01.userId = post_resp.json()['result']['userId']
    def test_api_002(self):
        session_id_str = str(TestApiHandler01.sessionId)
        user_id_str = str(TestApiHandler01.userId)
        post_resp = self.tools.get(
            path='small/user/verify/v1/receiveAddressList',
            headers={
                "userId":user_id_str,
                "sessionId":session_id_str
            }
        )
        print("收货地址列表状态码:",post_resp.status_code)
        print("收货地址列表响应:",post_resp.json())
        TestApiHandler01.id = post_resp.json()['result'][0]['id']
        print("收货地址列表ID",post_resp.json()['result'][0]['id'])
    def test_api_003(self):
        post_resp = self.tools.get(
            path='small/commodity/v1/commodityList'
        )
        print("维度电商首页商品列表响应状态码:", post_resp.status_code)
        print("维度电商首页商品列表响应数据:", post_resp.text)

        print("商品commodityId值:", post_resp.json()['result']['rxxp']['commodityList'][0]['commodityId'])
        TestApiHandler01.commodityId = post_resp.json()['result']['rxxp']['commodityList'][0]['commodityId']
        print("商品价格值:", post_resp.json()['result']['rxxp']['commodityList'][0]['price'])
        TestApiHandler01.price = post_resp.json()['result']['rxxp']['commodityList'][0]['price']
    #     断言状态码为200
        assert post_resp.status_code == 200
    def test_api_004(self):
        session_id_str = str(TestApiHandler01.sessionId)
        user_id_str = str(TestApiHandler01.userId)
        id_str = TestApiHandler01.id
        # 正确格式化 orderInfo - 使用 JSON 字符串
        order_info_json = '[{"commodityId":141,"amount":1}]'  # 已经是字符串格式

        post_resp = self.tools.post(
            path='small/order/verify/v1/createOrder',
            headers={
                "userId": user_id_str,
                "sessionId": session_id_str
            },
            params={
                "orderInfo": order_info_json,  # 直接使用字符串
                "totalPrice": 99.0,
                "addressId": id_str
            }
        )
        print("创建订单状态码:", post_resp.status_code)
        print("创建订单响应:", post_resp.json())
if __name__ == '__main__':
    pytest.main()




    # def test_api_login_002(self):
    #     # 将userId转换为字符串，因为HTTP头部需要字符串值
    #     session_id_str = str(TestApiHandler01.sessionId)
    #     user_id_str = str(TestApiHandler01.userId)
    #     post_resp = self.tools.post(
    #         path='small/user/verify/v1/addReceiveAddress',
    #         headers={"userId":user_id_str,
    #                  "sessionId":session_id_str},
    #         data={ "realName":"小新",
    #                 "phone":"17518836160",
    #                 "address":"北京海淀",
    #                 "zipCode":"101010"
    #                }
    #     )
    #     print("维度电商添加地址响应状态码:", post_resp.status_code)
    #     print("维度电商添加地址响应数据:", post_resp.text)
    # #     # 断言状态码为200
    #     assert post_resp.status_code == 200
    # def test_api_login_003(self):
    #     session_id_str = str(TestApiHandler01.sessionId)
    #     user_id_str = str(TestApiHandler01.userId)
    #     post_resp = self.tools.post(
    #         path='small/user/verify/v1/addReceiveAddress',
    #         headers={"userId": user_id_str,
    #                  "sessionId": session_id_str},
    #         data={"id":"9923"}
    #     )
    #     print("删除地址响应状态码:", post_resp.status_code)
    #     print("删除地址响应数据:", post_resp.text)
    #     # 断言状态码为200
    #     assert post_resp.status_code == 200



